#! /usr/bin/env python
# -*- coding: utf-8 -*-
# vim:fenc=utf-8
#
# Copyright © 2020 crane <crane@sharp.com>
#
# Distributed under terms of the MIT license.

"""

"""

class Solution:
    """
    @param pattern: a string, denote pattern string
    @param teststr: a string, denote matching string
    @return: an boolean, denote whether the pattern string and the matching string match or not
    """
    def wordPattern(self, pattern, teststr):
        # word 和 char/代数 之间是一对一的关系: 所以需要双向map来验证.
        # 一对一的关系: 需要两个map维护相应的对应关系.

        c2word = {}
        word2c = {}

        words = teststr.split()

        if len(pattern) != len(words):
            return False

        for idx in range(len(pattern)):
            real_c    = pattern[idx]
            real_word = words[idx]

            match_word = c2word.get(real_c)
            match_c    = word2c.get(real_word)

            if match_word is None and match_c is None:
                c2word[real_c]    = real_word
                word2c[real_word] = real_c
                continue

            if match_word is None or match_c is None:
                return False

            if match_c == real_c:
                # assert match_word == real_word
                continue

        return True


def test():
    s = Solution()

    ret = s.wordPattern("abbd", "dot cat cat cat")
    print(ret)

    ret = s.wordPattern("abbb", "dot cat cat cat")
    print(ret)


def main():
    print("start main")
    test()

if __name__ == "__main__":
    main()
